package info.gridworld.actor;

import info.gridworld.grid.Location;

import java.util.ArrayList;

public class KingCrab extends CrabCritter {

	private int getDistance(Location loc1, Location loc2) {
		int x1 = loc1.getRow();
		int y1 = loc1.getCol();
		int x2 = loc2.getRow();
		int y2 = loc2.getCol();
		double dist = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) + .5;
		return (int) Math.floor(dist);
	}


	private boolean moveAway(Actor a) {
		ArrayList<Location> locations = getGrid().getEmptyAdjacentLocations(
				a.getLocation());

		for (Location loc : locations) {
			if (getDistance(getLocation(), loc) > 1) {
				a.moveTo(loc);
				return true;
			}
		}
		return false;
	}


	@Override
	public void processActors(ArrayList<Actor> actors) {
		for (Actor a : actors) {
			if (!moveAway(a)) {
				a.removeSelfFromGrid();
			}
		}
	}

}
